package com.marker.mo.amapsmoothmarker;

import android.view.animation.LinearInterpolator;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.RotateAnimation;
import com.amap.api.maps.model.animation.TranslateAnimation;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class MySmoothMoveMarker {
    private AMap mAMap;
    private long duration = 10000L;
    private LinkedList<LatLng> points = new LinkedList();
    private LinkedList<Double> eachDistance = new LinkedList();
    private double totalDistance = 0.0D;
    private double remainDistance = 0.0D;
    private LatLng endPoint;
    private LatLng lastEndPoint;
    private Marker marker = null;
    private BitmapDescriptor descriptor;
    private TranslateAnimation animation;
    private int index = 0;
    private boolean useDefaultDescriptor = false;
    private Thread moveThread = null;
    private Timer timer = null;
    boolean exitFlag = false;
    private MoveListener moveListener;
    private LatLng tempPosition;

    /**
     * 自定义SmoothMoveMarker
     */
    public MySmoothMoveMarker(AMap var1) {
        this.mAMap = var1;
    }

    public void setPoints(List<LatLng> var1) {
        this.points.clear();
        Iterator var2 = var1.iterator();

        while (var2.hasNext()) {
            LatLng var3 = (LatLng) var2.next();
            this.points.add(var3);
        }

        if (var1.size() > 1) {
            this.endPoint = (LatLng) var1.get(var1.size() - 1);
            this.lastEndPoint = (LatLng) var1.get(var1.size() - 2);
        }

        this.eachDistance.clear();
        this.totalDistance = 0.0D;

        for (int var5 = 0; var5 < var1.size() - 1; ++var5) {
            double var7 = (double) AMapUtils.calculateLineDistance((LatLng) var1.get(var5), (LatLng) var1.get(var5 + 1));
            this.eachDistance.add(Double.valueOf(var7));
            this.totalDistance += var7;
        }

        this.remainDistance = this.totalDistance;
        LatLng var6 = (LatLng) this.points.removeFirst();
        if (this.marker != null) {
            this.marker.setPosition(var6);
            this.checkMarkerIcon();
        } else {
            if (this.descriptor == null) {
                this.useDefaultDescriptor = true;
            }

            this.marker = this.mAMap.addMarker((new MarkerOptions()).belowMaskLayer(true).position(var6).icon(this.descriptor).title("").anchor(0.5F, 0.5F));
        }

    }

    private void checkMarkerIcon() {
        if (this.useDefaultDescriptor) {
            if (this.descriptor == null) {
                this.useDefaultDescriptor = true;
            } else {
                this.marker.setIcon(this.descriptor);
                this.useDefaultDescriptor = false;
            }
        }

    }

    public void setTotalDuration(int var1) {
        this.duration = (long) (var1 * 1000);
    }

    long var3;// 动画时间
    LatLng var9;

    public void startSmoothMove() {
        if (this.points.size() >= 1) {
            this.index = 0;
            this.exitFlag = false;
            if (this.moveThread != null) {
                this.moveThread.interrupt();
            }

            this.moveThread = new Thread(new Runnable() {
                public void run() {
                    this.a();
                }

                private void a() {
                    try {
                        if (MySmoothMoveMarker.this.points.size() < 1) {
                            MySmoothMoveMarker.this.setEndRotate();
                            return;
                        }

                        double var1 = ((Double) MySmoothMoveMarker.this.eachDistance.poll()).doubleValue();
                        var3 = (long) ((double) MySmoothMoveMarker.this.duration * (var1 / MySmoothMoveMarker.this.totalDistance));
                        MySmoothMoveMarker.this.remainDistance = MySmoothMoveMarker.this.remainDistance - var1;
                        if (MySmoothMoveMarker.this.moveListener != null) {
                            if (MySmoothMoveMarker.this.remainDistance < 0.0D) {
                                MySmoothMoveMarker.this.remainDistance = 0.0D;
                            }

                            MySmoothMoveMarker.this.moveListener.move(MySmoothMoveMarker.this.remainDistance);
                        }

                        LatLng var5 = MySmoothMoveMarker.this.marker.getPosition();
                        if (MySmoothMoveMarker.this.tempPosition != null) {
                            float var6 = AMapUtils.calculateLineDistance(MySmoothMoveMarker.this.tempPosition, var5);
                            if (var6 < 10.0F) {
                                var5 = (LatLng) MySmoothMoveMarker.this.points.poll();
                                MySmoothMoveMarker.this.marker.setPosition(var5);
                            }
                        }
                        // 上一次车辆平移角度
                        float lastRotate = getRotate(tempPosition,var5);
                        MySmoothMoveMarker.this.tempPosition = var5;
                        var9 = (LatLng) MySmoothMoveMarker.this.points.poll();
                        if (MySmoothMoveMarker.this.timer != null) {
                            MySmoothMoveMarker.this.timer.cancel();
                        }
                        MySmoothMoveMarker.this.timer = new Timer();
                        MySmoothMoveMarker.this.timer.schedule(new TimerTask() {
                            public void run() {
                                if (MySmoothMoveMarker.this.points.size() > 0) {
//                                    MySmoothMoveMarker.access$1008(MySmoothMoveMarker.this);
                                    a();
                                } else {
                                    MySmoothMoveMarker.this.setEndRotate();
                                    moveListener.stop(marker.getPosition());
                                }

                            }
                        }, var3);
                        // 当前车辆平移角度
                        float var7 = MySmoothMoveMarker.this.getRotate(var5, var9);

                        MySmoothMoveMarker.this.marker.setRotateAngle(360.0F - var7 + MySmoothMoveMarker.this.mAMap.getCameraPosition().bearing);
                        RotateAnimation rotateAnimation = new RotateAnimation(360.0F - lastRotate + MySmoothMoveMarker.this.mAMap.getCameraPosition().bearing,360.0F - var7 + MySmoothMoveMarker.this.mAMap.getCameraPosition().bearing);
                        rotateAnimation.setInterpolator(new LinearInterpolator());
                        rotateAnimation.setDuration(var3 / 5);
                        if (MySmoothMoveMarker.this.exitFlag || Thread.interrupted()) {
                            MySmoothMoveMarker.this.marker.setAnimation((Animation) null);
                            return;
                        }
                        MySmoothMoveMarker.this.marker.setAnimation(rotateAnimation);
                        MySmoothMoveMarker.this.marker.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart() {

                            }

                            @Override
                            public void onAnimationEnd() {
                                MySmoothMoveMarker.this.animation = new TranslateAnimation(var9);
                                MySmoothMoveMarker.this.animation.setInterpolator(new LinearInterpolator());
                                MySmoothMoveMarker.this.animation.setDuration(4 *var3 /5);
                                if (MySmoothMoveMarker.this.exitFlag || Thread.interrupted()) {
                                    MySmoothMoveMarker.this.marker.setAnimation((Animation) null);
                                    return;
                                }

                                MySmoothMoveMarker.this.marker.setAnimation(MySmoothMoveMarker.this.animation);
                                MySmoothMoveMarker.this.marker.startAnimation();
                            }
                        });
                        MySmoothMoveMarker.this.marker.startAnimation();
                    } catch (Throwable var8) {
                        var8.printStackTrace();
                    }

                }
            });
            this.moveThread.start();
        }
    }

    private void setEndRotate() {
        if (this.marker != null) {
            float var1 = this.getRotate(this.lastEndPoint, this.endPoint);
            this.marker.setRotateAngle(360.0F - var1 + this.mAMap.getCameraPosition().bearing);
            this.marker.setPosition(this.endPoint);
        }
    }

    private float getRotate(LatLng var1, LatLng var2) {
        if (var1 != null && var2 != null) {
            double var3 = var1.latitude;
            double var5 = var2.latitude;
            double var7 = var1.longitude;
            double var9 = var2.longitude;
            float var11 = (float) (Math.atan2(var9 - var7, var5 - var3) / 3.141592653589793D * 180.0D);
            return var11;
        } else {
            return 0.0F;
        }
    }

    public void stopMove() {
        this.exitFlag = true;
        if (this.marker != null) {
            this.marker.setAnimation((Animation) null);
        }

        if (this.timer != null) {
            this.timer.cancel();
        }

        if (this.moveThread != null) {
            this.moveThread.interrupt();
        }

        this.index = 0;
    }

    public Marker getMarker() {
        return this.marker;
    }

    public LatLng getPosition() {
        return this.marker == null ? null : this.marker.getPosition();
    }

    public int getIndex() {
        return this.index;
    }

    public void destroy() {
        this.stopMove();
        if (this.descriptor != null) {
            this.descriptor.recycle();
        }

        if (this.marker != null) {
            this.marker.destroy();
            this.marker = null;
        }

        this.points.clear();
        this.eachDistance.clear();
    }

    public void setDescriptor(BitmapDescriptor var1) {
        if (this.descriptor != null) {
            this.descriptor.recycle();
        }

        this.descriptor = var1;
        if (this.marker != null) {
            this.marker.setIcon(var1);
        }

    }

    public void setRotate(float var1) {
        if (this.marker != null) {
            this.marker.setRotateAngle(360.0F - var1);
        }

    }

    public void setVisible(boolean var1) {
        if (this.marker != null) {
            this.marker.setVisible(var1);
        }

    }

    public void setTempPositionNull() {
        this.tempPosition = null;
    }

    public void setMoveListener(MoveListener var1) {
        this.moveListener = var1;
    }

    public interface MoveListener {
        void move(double var1);

        void stop(LatLng latLng);
    }
}
